var pagNum1 = 0;
var preCateId1 = '';
var userId = userid;
jQuery(function () {

    loadCateRemote('0');
    $("#toLogin").click(function(){
        window.location.href = '/LianniuWeb/login.do';
    })
});
var nav_click = function() {
    $("#navlist_trend li").on('click', function () {
        $('#prodlist_trend').html('');
        pagNum1 = 0;
        loadCateRemote($(this).attr('cateId'));
        $("#navlist_trend li").removeClass("select").removeClass("sel");
        $(this).addClass("select").addClass("sel");

    });
    /*开始采购大分类点击效果-结束*/
}
var leff_click = function(){
    /*开始采购小分类点击效果-开始*/
    $("#leftlist_trend li").on('click', function () {
        //console.log($(this).attr('class'));
        if (null == $(this).attr('class') || $(this).attr('class') == '') {
            pagNum1 = 0;
            $('#prodlist_trend').html('');
            //console.log('小分类调用了loadProductRemote');
            $("#leftlist_trend li").removeClass("sel");
            $(this).addClass("sel");
            loadProductRemote($(this).attr('cateId'));
        }
    });
}
var ranked = false
var loadCateRemote = function (cateId) {
    console.log(cateId)
    $.ajax({
        url: server_url + "buyer/product/rest/buyerCateLoad.do?cateId=" + cateId,
        type: 'get',
        data:{ran:Math.random()},
        ansyc: false,
        error: function () {
            alert(contants.systemError)
        },
        success: function (msg) {
            var data = msg.data;
            if (cateId == '0') {
                dealCateTopData(data);
                nav_click();
            } else {
                dealCateLeftData(data);
                leff_click();
                if( !ranked){
                    rank_slide();
                }
            }
        }
    });
}
var b = false;
var loadProductRemote = function (cateId) {
    console.log(cateId)
    if (preCateId1 != cateId) {
        preCateId1 = cateId;
        pagNum1 = 0;
    }
    if (b) {
        return;
    }
    if (pagNum1 == 0){
        $("#prodlist_trend").children().remove()
    }
    var searchName = $("#searchName").val();
    b = true;
    $.ajax({
        url: server_url + "buyer/product/rest/getTrendProductList.do?cateId=" + cateId + "&userId=" + userid + "&currPage=" + pagNum1,
        data:{searchName:searchName,ran:Math.random()},
        type: 'get',
        ansyc: false,
        error: function () {
            alert(contants.systemError)
        },
        success: function (msg) {
            var data = msg.data;
            b = false;
            console.log(data)
            dealProductData(data);
        }
    });
}
var dealCateTopData = function (data) {
    var topCate = '<li class="find_nav_cur" cateId="-1" class="sel"><a href="javascript:void(0)">全部</a></li>';
    for (var i = 0; i < data.length; i++) {
        topCate += '<li cateId="' + data[i].cateId + '" ><a href="javascript:void(0)">' + data[i].cateName + '</a></li>';
    }
    topCate += '<li class="sideline" style="width: 65px; left: 13px;"></li>';
    $('#navlist_trend').html(topCate);
    loadCateRemote("-1");
}
var dealCateLeftData = function (data) {
    var leftCate = '';
    if (data != null && data.length > 0) {
        for (var i = 0; i < data.length; i++) {
            if (i == 0) {
                leftCate += '<li cateId="' + data[i].cateId + '" class="sel">' + data[i].cateName + '</li>';
            } else {
                leftCate += '<li cateId="' + data[i].cateId + '">' + data[i].cateName + '</li>';
            }
        }
        $('#leftlist_trend').html(leftCate);
        pagNum1 = 0;
        loadProductRemote(data[0].cateId);
    }else{
        $('#leftlist_trend').html("");
        pagNum1 = 0;
    }

}
var dealProductData = function (data) {
    var s = '';
    $("#more").remove();
    if (data != null && data.length > 0) {
        for (var i = 0; i < data.length; i++) {
            s += '<div class="det_box" style="display: block">\
                <img src="' + data[i].picturePath1 + '" alt="" class=""/>\
                <div class="">\
                <div class="con_tit">\
                <p>' + data[i].productName + '</p>\
                <span>￥' + data[i].price + '/头</span>\
                </div>\
                <p class="con_normal">类别：' + data[i].specName + '</p>\
                <p class="con_normal">成交量：' + data[i].overPlusNum + '头</p>\
                <p class="con_normal">货源地：' + data[i].areaName + '</p>\
                <p class="con_normal">'+ data[i].addTime +'</p>\
                </div></div>';
        }
        $("#prodlist_trend").append(s);

        if(userid == null || userid == ''){
            $("#toLogin").show();
        }else{
            var more = '<div class="det_box" id="more" style="display: block" onclick="loadProductRemote('+preCateId1+');"> \
            <p style="\
        text-align: center;margin-left: -56px;;\
        ">加载更多</p>\
        </div>';
            $("#prodlist_trend").append(more);
        }
        pagNum1++;
    } else {
        if (pagNum1 < 1) {
            s = "<li style='width:5.6rem; text-align:center; float:right; border-bottom:none;'>" +
            "<p style='font-size:0.3rem; padding:50% 5% 50% 5%;'>该分类暂时没有合适商品，请在我的-问题反馈中提交需求 &nbsp;" +
            "</p>" +
            "</li>";
            $("#prodlist_trend").html(s);
        }else{
            var more = '<div class="det_box" id="more" style="display: block"> \
            <p style="\
        text-align: center;margin-left: -56px;;\
        ">没有更多了</p>\
        </div>';
            $("#prodlist_trend").append(more);
        }

    }

}
var setSku = function(skuId){
    localStorage.setItem('skuId', skuId);
    window.location.href=server_url+"buyer/product/productDetail.do";
}

var search = function(){
    pagNum1 = 0;
    var cateId = $("#leftlist_trend .sel").attr("cateId");
    loadProductRemote(cateId)
}
document.onkeydown = function (event){
    if (event.keyCode==13) //回车键的键值为13
        search();
};